﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Web.Script.Serialization;
using System.Text.RegularExpressions;

namespace XJX.Page
{

    /// <summary>
    ///JSON 的摘要说明
    /// </summary>
    public class JSON
    {
        public JSON()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
    }

    [Serializable]
    public class JsonResult
    {
        private StatusMessageType _messageType;
        private string _messageContent;


        /// <summary>  
        ///   
        /// </summary>  
        /// <param name="messageType"></param>  
        /// <param name="messageContent"></param>  
        /// <param name="data"></param>  
        public JsonResult(StatusMessageType messageType, string messageContent, object data = null)
        {
            _messageType = messageType;
            _messageContent = messageContent;
            Data = data;
        }

        public StatusMessageType MessageType
        {
            get { return _messageType; }
            set { _messageType = value; }
        }

        public string MessageContent
        {
            get { return _messageContent; }
            set { _messageContent = value; }
        }

        public object Data { get; set; }

        public string ToJson()
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            var json = serializer.Serialize(this);

            //string p = @"\\/Date(\d+)\\/";  
            //MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString);  
            //Regex reg = new Regex(p);  
            //json = reg.Replace(json, matchEvaluator);  
            return json;
        }

        private static string ConvertJsonDateToDateString(Match m)
        {
            string result = string.Empty;
            DateTime dt = new DateTime(1970, 1, 1);
            dt = dt.AddMilliseconds(long.Parse(m.Groups[1].Value));
            dt = dt.ToLocalTime();
            result = dt.ToString("d");
            return result;
        }
    }


    /// <summary>  
    /// 提示消息类别  
    /// </summary>  
    public enum StatusMessageType
    {
        /// <summary>  
        /// 权限不足  
        /// </summary>  
        NoAccess = -2,
        /// <summary>  
        /// 错误  
        /// </summary>  
        Error = -1,
        /// <summary>  
        /// 成功  
        /// </summary>  
        Success = 1,

        /// <summary>  
        /// 提示信息  
        /// </summary>  
        Hint = 0,

        /// <summary>  
        /// 系统限制  
        /// </summary>  
        Restrict = 10,

        /// <summary>  
        /// 未知错误  
        /// </summary>  
        OtherError = 999

    }
}